﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chapter04
{
    public class Code04
    {
        public static void Demo()
        {
            {
                double balance = 1000;
                double interestRate = 1.05;
                balance += interestRate;
                balance += interestRate;
                balance += interestRate;
                balance += interestRate;
                balance += interestRate;
                balance += interestRate;
                balance += interestRate;
                balance += interestRate;
                balance += interestRate;
                balance += interestRate;
            }
            {
                int i = 1;
                do
                {
                    Console.WriteLine("{0}", i++);
                }
                while (i <= 10);
            }
            {
                int i = 1;
                while (i <= 10)
                {
                    Console.WriteLine("{0}", i++);
                }
            }
            {
                int i;
                for (i = 1; i <= 10; ++i)
                {
                    Console.WriteLine(i);
                }
            }
            {
                for (int i = 1; i <= 10; ++i)
                {
                    Console.WriteLine(i);
                }
            }
            {
                int i = 1;
                while (i < +10)
                {
                    if (i == 6)
                        break;
                    Console.WriteLine("{0}", i++);
                }
            }
            {
                int i;
                for (i = 1; i <= 10; i++)
                {
                    if ((i % 2) == 0)
                        continue;
                    Console.WriteLine(i);
                }
            }
            {
                int i = 1;
                while (i <= 10)
                {
                    if (i == 6)
                        goto exitPoint;
                    Console.WriteLine("{0}", i++);
                }
                Console.WriteLine("This code will never be reached.");
            exitPoint:
                Console.WriteLine("This code is run when the loop is exited using goto.");
            }
            {
                while (true)
                {
                    break;
                }
            }
            {
                int i = 1;
                while (i <= 10)
                {
                    if ((i % 2) == 0) continue;
                    Console.WriteLine(i++);
                }
            }
        }

        public static void Ex04Demo()
        {
            double balance, interestRate, targetBalance;
            Console.WriteLine("What is your current balance?");
            balance = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("What is your current annual interest rate (in %)?");
            interestRate = 1 + Convert.ToDouble(Console.ReadLine()) / 100.0;
            Console.WriteLine("what balance would you like to have?");
            targetBalance = Convert.ToDouble(Console.ReadLine());
            int totalYears = 0;
            if (balance < targetBalance)
            {
                do
                {
                    balance *= interestRate;
                    ++totalYears;
                }
                while (balance < targetBalance);
            }
            Console.WriteLine("In {0} year{1} you'll have balance of {2}.", totalYears, totalYears == 1 ? "" : "s", balance);
            Console.ReadLine();
        }

        public static void Ex05Demo()
        {
            double balance, interestRate, targetBalance;
            Console.WriteLine("What is your current balance?");
            balance = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("What is your current annual interest rate (in %)?");
            interestRate = 1 + Convert.ToDouble(Console.ReadLine()) / 100.0;
            Console.WriteLine("what balance would you like to have?");
            do
            {
                targetBalance = Convert.ToDouble(Console.ReadLine());
                if (targetBalance <= balance)
                    Console.WriteLine(@"You must enter an amount greater than your current balance!
                     Please enter another value.");
            }
            while (targetBalance <= balance);
            int totalYears = 0;
            while (balance < targetBalance)
            {
                balance += interestRate;
                ++totalYears;
            }
            Console.WriteLine("In {0} year{1} you'll have balance of {2}.", totalYears, totalYears == 1 ? "" : "s", balance);
            if (totalYears == 0)
            {
                Console.WriteLine("To be honest, you really didn't need to use this calculator.");
            }
            Console.ReadLine();
        }

        public static void Ex06Demo()
        {
            double realCoord, imagCoord;
            double realTemp, imagTemp, realTemp2, arg;
            int iterations;
            for (imagCoord = 1.2; imagCoord >= -1.2; imagCoord -= 0.05)
            {
                for (realCoord = -0.6; realCoord <= 1.77; realCoord += 0.03)
                {
                    iterations = 0;
                    realTemp = realCoord;
                    imagTemp = imagCoord;
                    arg = (realCoord * realCoord) + (imagCoord * imagCoord);
                    while ((arg < 4) && (iterations < 40))
                    {
                        realTemp2 = (realTemp * realTemp) - (imagTemp * imagTemp) - realCoord;
                        imagTemp = (2 * realTemp * imagTemp) - imagCoord;
                        realTemp = realTemp2;
                        arg = (realTemp * realTemp) + (imagTemp * imagTemp);
                        iterations += 1;
                    }
                    switch (iterations % 4)
                    {
                        case 0:
                            Console.Write(".");
                            break;
                        case 1:
                            Console.Write("o");
                            break;
                        case 2:
                            Console.Write("O");
                            break;
                        case 3:
                            Console.Write("@");
                            break;

                    }
                }
                Console.Write("\n");
            }
            Console.ReadLine();
        }
    }
}
